package com.pg.excel.ui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;

import sun.font.FontDesignMetrics;

/**
 * Copyright (C) 2019/8/10
 *
 * @author fred
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public class MyButton extends JButton {
    private String text;
    private float alpha = 1f;
    private boolean isMouseEntered = false;
    private boolean isPressed = false;

    /**
     * 边框颜色
     */
    private Color borderColor = new Color(120,120,120);
    /**
     * 正常的背景颜色
     */
    private Color backgroundColor1 = new Color(60,63,65);
    /**
     * 鼠标进入时的背景颜色
     */
    private Color backgroundColor2 = new Color(76,80,82);
    /**
     * 按下后的背景颜色
     */
    private Color backgroundColor3 = new Color(83,87,89);


//    private Paint bg1 = new GradientPaint();

    public MyButton(){
        this(null);
    }

    public MyButton(String buttonText) {
         super(buttonText);
        this.text = buttonText;

        initStyle();
        //添加鼠标监听
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                //当鼠标进入时,鼠标进入状态改为TRUE，并重绘按钮  
                isMouseEntered = true;
                repaint();

            }

            @Override
            public void mouseExited(MouseEvent e) {
                isMouseEntered = false;
                repaint();
            }

            @Override


            public void mousePressed(MouseEvent evt) {
                isPressed = true;
                repaint();
            }

            @Override


            public void mouseReleased(MouseEvent evt) {
                isPressed = false;
                repaint();
            }

        });
    }

    private void initStyle() {
        setOpaque(false);
        setBorder(BorderFactory.createLineBorder(borderColor, 1));
        setBorderPainted(true);
        setFocusable(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        AlphaComposite composite = AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, alpha);
        g2.setComposite(composite);
//        drawButtonBackground(g2, this);
        if (isMouseEntered){
            //鼠标进入
            if (isPressed){
                //按下
                setBorder(BorderFactory.createLineBorder(borderColor, 1));
                g2.setPaint(new GradientPaint(0,0,backgroundColor3,getWidth(),getHeight(),backgroundColor3));
                g2.fillRect(0,0,getWidth(),getHeight());
            }else{
                setBorder(BorderFactory.createLineBorder(borderColor, 1));
                g2.setPaint(new GradientPaint(0,0,backgroundColor2,getWidth(),getHeight(),backgroundColor2));
                g2.fillRect(0,0,getWidth(),getHeight());
            }
        }else{
            setBorder(BorderFactory.createLineBorder(borderColor, 1));
            g2.setPaint(new GradientPaint(0,0,backgroundColor1,getWidth(),getHeight(),backgroundColor1));
            g2.fillRect(0,0,getWidth(),getHeight());
        }
        drawText(g2);
    }

    private void drawText(Graphics2D g2){
        if (text == null){
            text = getText();
        }
        g2.setColor(getForeground());
        FontDesignMetrics metrics = FontDesignMetrics.getMetrics(getFont());
        int strWidth = metrics.stringWidth(text);
        int strHeight = metrics.getHeight();
        int x = (getWidth()-strWidth)/2;
        int y = (getHeight()-strHeight)/2+metrics.getAscent();
        g2.drawString(text, x, y);
    }

}
